<div class="row animate fadeIn">
    <form class="form-horizontal" id="trans_inv_form">
    <div class="ibox-title" style="border:none;"><h5 style="color: #00afef"><i>Add</i></h5>
        <div class="ibox-tools">    
             <button class="btn btn-primary btn-sm save-btn-tab" id="submit-uom">Save</button>
             <a id="cancel_add" class="btn btn-sm btn-outline btn-info cancel-btn-tab">Cancel</a>
        </div>
     </div>
     <div class="ibox-content">
        
            <div class="form-group">
                <label class="col-lg-3 control-label">Item Number</label>
                <div class="col-lg-4">
                     <select class="chosen-select" name="tr_item_id">
                        <?php foreach ($items as $item) { ?>
                            <option value="<?php echo $item->item_id; ?>"><?php echo $item->item_num.'  ('.$item->description.')'; ?></option>
                        <?php } ?>
                      </select>
                </div>
             </div>
             <div class="form-group">
                <label class="col-lg-3 control-label">Sub-Inventory Name</label>
                <div class="col-lg-5">
                    <select class = "chosen-select" name="trans_sub_id" id ="tr_sub_id">
                      <option value="" id="empty-option">Select Sub-Inventory</option>
                          <?php foreach ($subs as $sub) {?>
                      <option value ="<?php echo $sub->inv_sub_id; ?>"><?php echo $sub->inv_sub_code.'  ('.$sub->inv_sub_name.')'; ?></option>
                      <?php } ?>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-3 control-label">Locator Code</label>
                <div class="col-lg-5">
                  <select class="chosen-select" name="trans_locate_id" id ="tr_locate_id">
                        <option value="" id="empty-option">Select Module Menu</option>
                  </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-3 control-label">Item Quantity</label>
                <div class="col-lg-5">
                    <input type="text" id="tr_item_qty"class="form-control" name="trans_item_qty">
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-3 control-label">Reorder Level</label>
                <div class="col-lg-5">
                    <input type="text" id="tr_item_RL" class="form-control" name="trans_item_RL">
                </div>
            </div>
      </div>
    </form>
    
 </div>
 <script>
    $(document).ready(function(){
    $('#trans_inv_form').data('serialize',$('#trans_inv_form').serialize());
    $('#add_locator_sched').hide();
    $(".chosen-select").chosen({width:"100%;"}); 



    $('#tr_sub_id').change(function(){
                    var id = $('#tr_sub_id').val();
                    console.log(id);
                    $.ajax({
                    type: "POST",
                    url: "<?php echo base_url($this->session->userdata('forajax') . '/search_locators'); ?>",
                    data: { "sub_ids" : id },
                    dataType: 'json',
                    success: function(e){
                        console.log(e);
                        var tmp = "";
                        $.each(e, function(i, item) {
                            tmp += "<option value='"+e[i].locator_id+"'>"+e[i].locator_code+"  ("+e[i].locator_description+")</option>";
                        });
                        
                        $('#tr_locate_id').html(tmp);
                        $('select').trigger('chosen:updated');
                    }
                    });
                });





    $.validator.setDefaults({ ignore: ":hidden:not(select)" });
    $('#trans_inv_form').validate({
        rules :{
            tr_item_id:{
                required: true
            },
            trans_sub_id:{
                required: true
            },
             trans_locate_id:{
                required: true
            },
            trans_item_qty:{
                required: true,
                number: true
                
            }
        },submitHandler: function(form){
          dataString = $("#trans_inv_form").serialize();
          $.ajax({
            type: "POST",
            url: "<?php echo base_url($this->session->userdata('forajax').'/insert_trans_inv'); ?>",
            data: dataString,
            dataType: 'json',
          success: function(data){
            if(data[0].error == 1){
                notifymsg("danger",data[0].msg);
            }else{
                notifymsg("success",". . .New Sub-Inventory Added Successfully");
                $('#cancel_add').html('Complete');
            }
          }      
         });   
        }
      });



      function alert_cancel(){
            if($('#trans_inv_form').serialize()!=$('#trans_inv_form').data('serialize')){
                swal({
                    title: "Are you sure?",
                    text: "Your work will not be save",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "Leave",
                    cancelButtonText: "Stay",
                    closeOnConfirm: true,
                    closeOnCancel: true },
                function (isConfirm) {
                    if (isConfirm) {
                        close_add(); active_search();
                        notifymsg("hide","hide");
                    } else {
                        swal("Cancelled");
                    }
                });
                }else{
                close_add(); active_search();
                notifymsg("hide","hide");
            }
        } 
        $('#cancel_add').click(function () {
          if($('#cancel_add').html() == "Complete"){
            close_add(); active_search();
            notifymsg("hide","hide");
          }else{            
            alert_cancel();
          }
        });


      $('#addX').click(function(){
            alert_cancel();      
       });
    });
 </script>  